define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope,$rootScope, T_address_add_yh, GoPage, GoDialog, Auth, $stateParams) {
		$scope.id = $stateParams.id;
		$scope.datas = new Object();
		$scope.datas.account_name = "";
		$scope.datas.bank_name = "";
		$scope.datas.bank_no = "";
		$scope.datas.phone = "";
		$scope.datas.def_bank = false;//是否默认
		$scope.title = "添加银行卡";
		
		$scope.$on('$ionicView.beforeEnter', function() {
			var Infos = Auth.get_loginInfo();
			$scope.issue_type = Infos.issue_type;//1买家    2卖家
		}, false);
		//页面初始化
		$scope.init_data = function() {
			if($scope.id != ''){
				T_address_add_yh.bank_detail($scope.id, function(data) {
					console.log(data)
					$scope.datas = data;
					$scope.title = "编辑银行卡"
					if(data.def_bank){//如果是默认，则不显示勾选
						$scope.isShowDefualt = true;
					}
				})
			}
		}
		$scope.init_data();

		//清空输入
		$scope.clearInput = function(type){
			if(type==1) $scope.datas.account_name = "";
			if(type==2) $scope.datas.bank_name = "";
			if(type==3) $scope.datas.bank_no = "";
			if(type==4) $scope.datas.phone = "";
		}
		
		//是否默认
		$scope.handleIsDefualt = function(){
			$scope.datas.def_bank = !$scope.datas.def_bank;
		}

		//提交
		$scope.onshow_address = function() {
			if(!$scope.datas.account_name) {
				GoDialog.toast("请输入持卡人姓名");
				return
			} else if(!$scope.datas.bank_name) {
				GoDialog.toast("请输入开户银行");
				return
			} else if(!$scope.datas.bank_no) {
				GoDialog.toast("请输入银行账号");
				return
			} else if(!$scope.datas.phone) {
				GoDialog.toast("请输入联系电话");
				return
			}
			if(isNaN($scope.datas.phone)) {
				GoDialog.toast("联系手机格式不正确");
				return
			}
			if($scope.datas.phone.length != 11) {
				GoDialog.toast("联系手机长度不正确");
				return
			}
			T_address_add_yh.bankdata_edit($scope.id, $scope.datas.account_name, $scope.datas.bank_name, $scope.datas.bank_no, $scope.datas.phone, $scope.datas.def_bank, function(data) {
				if(data.success == true) {
					GoDialog.toast("保存成功");
					GoPage.go_back(-1); //返回上一页
					//注册了全局广播
					$rootScope.$broadcast('editBankEvent');
				} else {
					GoDialog.alert("提示",data.msg);
				}
			})
		}
		
		
	}

	controller.$inject = ['$scope','$rootScope', 'T_address_add_yh', 'GoPage', 'GoDialog', 'Auth','$stateParams'];

	return app.registerController("address_add_yhctrl",controller);
});